//
//  RHEnterpriseBasicInfoCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/7/28.
//

#import "RHEnterpriseBasicInfoCell.h"

@interface RHEnterpriseBasicInfoCell()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property(nonatomic,strong)UIView *footView;
@property(nonatomic,strong)NSMutableArray *imgArr;
@property(nonatomic,assign)CGFloat itemHeight;
//公司信息
@property(nonatomic ,strong)NSArray *enterpreiseInfoArr;
//营业执照信息
@property(nonatomic,strong)NSArray *lisenceArr;

@end
@implementation RHEnterpriseBasicInfoCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

-(void)setInfoModel:(RHApplicationDetailModel *)infoModel
{
    if(!infoModel)return;
    _infoModel = infoModel;
    //_titleArr = @[@"country/region",@"company name",@"company type",@"legal person",@"contact information",@"address",@"Number of people",@"website",@"business scope"];
    self.enterpreiseInfoArr = @[@{@"title":@"brand name",@"content":ValidNullData(_infoModel.brandName)},
                         @{@"title":@"company name",@"content":_infoModel.companyName},
                         @{@"title":@"company type",@"content":_infoModel.companyType},
                         @{@"title":@"Date of establishment",@"content":ValidNullData(_infoModel.registerData)},
                         @{@"title":@"legal person",@"content":_infoModel.companyCreate},
                         @{@"title":@"contact information",@"content":_infoModel.contact},
                         @{@"title":@"address",@"content":_infoModel.companyAddress},
                         @{@"title":@"Number of people",@"content":_infoModel.employeesNumber},
                         @{@"title":@"website",@"content":_infoModel.companyWeburl},
                         @{@"title":@"business scope",@"content":_infoModel.scope},];
    kWeakSelf;
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        NSInteger row = self.infoModel.licenseUrl.count%2>0?self.infoModel.licenseUrl.count/2+1:self.infoModel.licenseUrl.count/2;
        make.height.offset(kScaleW(45)*weakSelf.enterpreiseInfoArr.count + row * weakSelf.itemHeight+20*(row+1));
    }];
    for (UIImageView *img in self.imgArr) {
        img.hidden = YES;
    }
    //营业执照
    for (int i = 0; i<_infoModel.licenseUrl.count; i++) {
        if(i>=self.imgArr.count) break;
        RHlisence *lisense = _infoModel.licenseUrl[i];
        UIImageView *imageView = self.imgArr[i];
        imageView.hidden = NO;
        [imageView sd_setImageWithURL:[NSURL URLWithString:lisense.fileUrl]];
    }
    //法人信息
    NSInteger start = self.infoModel.licenseUrl.count;
    for (NSInteger i = 0; i<_infoModel.legalPersonFile.count; i++) {
        if(i+start>=self.imgArr.count) break;
        RHlisence *lisense = _infoModel.legalPersonFile[i];
        UIImageView *imageView = self.imgArr[i+start];
        imageView.hidden = NO;
        [imageView sd_setImageWithURL:[NSURL URLWithString:lisense.fileUrl]];
    }
    [self.tableView reloadData];
}
- (void)setupUI{
    self.backgroundColor = CFTabViewBgColor;
    self.contentView.backgroundColor = CFTabViewBgColor;
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    _tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
    _tableView.scrollEnabled = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.layer.cornerRadius = 8;
    _tableView.layer.masksToBounds = YES;
    [_tableView setBackgroundColor:UIColor.whiteColor];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
//    [_tableView registerClass:[RHMyApprovalListCell class] forCellReuseIdentifier:@"RHMyApprovalListCell"];
    [self.contentView addSubview:_tableView];
    
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    if (@available(iOS 15.0, *)) {
            UITableView.appearance.sectionHeaderTopPadding = 0;
            
    }
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 15, 0, 15));
    }];
    NSInteger count = 4;//总个数
    CGFloat leftRightMargin = 30;//左右间距
    CGFloat topBottomMargin = 20;//上下间距
    CGFloat itemMargin = 20;     //元素之间的间距
    CGFloat width = kUI_WINDOW_W-30;  //父视图总宽度
    CGFloat heigh = kUI_WINDOW_W*0.8; //父视图总高度
    NSInteger row_per = 2; //每行排多少个
    NSInteger col_per = 2;  //每列排多少个
    CGFloat item_width = (width - leftRightMargin*2 - itemMargin)/col_per;
    CGFloat item_height = (heigh - topBottomMargin*2 - itemMargin)/row_per;
    _itemHeight = item_height;
    
//    UIView *footerContainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W-20, kUI_WINDOW_W*0.8)];
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width, heigh)];
//    [footerContainView addSubview:footView];
    _tableView.tableFooterView = footView;
    footView.backgroundColor = [UIColor whiteColor];
    
    //创建4个img
    _imgArr = [NSMutableArray array];
    for (int i = 0; i< 4; i++) {
        UIImageView *img = [[UIImageView alloc]init];
        img.contentMode = UIViewContentModeScaleAspectFill;
        img.layer.cornerRadius = 8;
        img.layer.masksToBounds = YES;
        img.hidden = YES;
        [footView addSubview:img];
        img.backgroundColor = kRGB(240, 240, 240);
        NSInteger row = i/row_per;
        NSInteger col = i%row_per;
        CGFloat x = leftRightMargin + col*(item_width+itemMargin);
        CGFloat y = topBottomMargin + row*(item_height+itemMargin);
        img.frame = CGRectMake(x, y, item_width, item_height);
        [self.imgArr addObject:img];
//        NSLog(@"----->%@  img frame:%@",img,NSStringFromCGRect(img.frame));
    }
    
    

}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.enterpreiseInfoArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kScaleW(45);
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return  [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kUI_WINDOW_W, 0.01f)];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"baseSubCell"];
    if(cell==nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"baseSubCell"];
    }
    cell.textLabel.text = self.enterpreiseInfoArr[indexPath.row][@"title"];
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.detailTextLabel.text = self.enterpreiseInfoArr[indexPath.row][@"content"];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
    return cell;
}

@end
